package com.jtlrm.ckd.mvp.view.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.base.sdk.base.net.CommonObserver;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.acitvity.TitleBarActivity;
import com.jtlrm.ckd.entity.BanSuiEntity;
import com.jtlrm.ckd.entity.CodeEntity;
import com.jtlrm.ckd.entity.event.SelectBanSuiMessage;
import com.jtlrm.ckd.mvp.model.ResourceModel;
import com.jtlrm.ckd.mvp.view.adapter.BanSuiBingAdapter;
import com.jtlrm.ckd.util.EventBusUtil;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class BanSuiJiBingActivity extends TitleBarActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    BanSuiBingAdapter adapter;
    ResourceModel resourceModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected String getTitleText() {
        return "请选择伴随疾病";
    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_ban_sui_ji_bing);
    }

    @Override
    protected void initView() {
        adapter = new BanSuiBingAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(createItemDecoration());
        resourceModel = new ResourceModel();
    }

    @Override
    protected void obtainData() {
        showLoadingDialog("加载中");
        resourceModel.getCode(ResourceModel.KeyCode.BAN_SUI_JI_BING, new CommonObserver<List<CodeEntity>>() {
            @Override
            public void onError(int errType, String errMessage) {
                showToast(errMessage);
                dismissLoadingDialog();
            }

            @Override
            public void onResult(List<CodeEntity> data) {
                dismissLoadingDialog();
                if (data != null && data.size() > 0) {
                    List<BanSuiEntity> list = new ArrayList<>();
                    for (CodeEntity entity : data) {
                        BanSuiEntity banSuiEntity = new BanSuiEntity();
                        banSuiEntity.setId(entity.getId());
                        banSuiEntity.setName(entity.getName());
                        banSuiEntity.setChecked(false);
                        list.add(banSuiEntity);
                    }
                    adapter.addData(list);
                }

            }
        }, lifecycleSubject);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                BanSuiEntity entity = (BanSuiEntity) adapter.getData().get(position);
                ((BanSuiEntity) adapter.getData().get(position)).setChecked(!entity.isChecked());
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void initEvent() {
        titleBar.tvRight.setText("确定");
        titleBar.tvRight.setVisibility(View.VISIBLE);
        titleBar.tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<BanSuiEntity> list = adapter.getData();
                List<CodeEntity> result = new ArrayList<>();
                for (BanSuiEntity entity : list) {
                    if (entity.isChecked()) {
                        result.add(entity);
                    }
                }
                if (result.size() > 0) {
                    SelectBanSuiMessage message = new SelectBanSuiMessage();
                    message.setData(result);
                    EventBusUtil.postEvent(message);
                    finish();
                } else {
                    showToast("请选择伴随病");
                }
            }
        });
    }

    protected RecyclerView.ItemDecoration createItemDecoration() {
        return new DefaultItemDecoration(ContextCompat.getColor(this, R.color.margin_background),2,20, -1);
    }
}
